package cn.nucun.frame;

import cn.nucun.base.WindowsRem;
import cn.nucun.factory.ToolWindowsFactory;
import cn.nucun.utils.HttpUtils;
import cn.nucun.utils.PlugsUtils;
import cn.nucun.wrapper.LoginInDialog;
import com.alibaba.fastjson.JSONObject;
import com.intellij.openapi.ui.DialogWrapper;
import org.apache.commons.lang.StringUtils;

import javax.swing.*;
import java.awt.*;

import static cn.nucun.entity.constant.SocketConstant.HttpRequest.REQ_HTTP_CHECK_LOGIN;
import static cn.nucun.entity.constant.SocketConstant.Systems.SYS_USER_KEY;

/**
 * 登录窗体
 *
 * @author lx
 */
public class LoginInFrame {

    private JPanel north = new JPanel();
    private JPanel center = new JPanel();
    private JPanel south = new JPanel();

    private JLabel name = new JLabel("account:");
    private JTextField nameContent = new JTextField();
    private JLabel age = new JLabel("password:");
    private JTextField ageContent = new JTextField();

    public JPanel initNorth() {
        //定义表单的标题部分，放置到IDEA会话框的顶部位置
        JLabel title = new JLabel("CodingChat");
        //字体样式
        title.setFont(new Font("微软雅黑", Font.PLAIN, 26));
        //水平居中
        title.setHorizontalAlignment(SwingConstants.CENTER);
        //垂直居中
        title.setVerticalAlignment(SwingConstants.CENTER);
        north.add(title);
        return north;
    }

    public JPanel initCenter() {
        //定义表单的主体部分，放置到IDEA会话框的中央位置
        //一个简单的2行2列的表格布局
        center.setLayout(new GridLayout(2, 2));
        center.setPreferredSize(new Dimension(200, 60));
        //row1：姓名+文本框
        center.add(name);
        center.add(nameContent);
        //row2：密码+文本框
        center.add(age);
        center.add(ageContent);
        return center;
    }

    public JPanel initSouth(LoginInDialog loginInDialog) {
        //定义表单的提交按钮，放置到IDEA会话框的底部位置
        JButton submit = new JButton("GO");
        //水平居中
        submit.setHorizontalAlignment(SwingConstants.CENTER);
        //垂直居中
        submit.setVerticalAlignment(SwingConstants.CENTER);
        south.add(submit);
        //按钮事件绑定
        submit.addActionListener(e -> {
            //获取到name和age
            String name = nameContent.getText().trim();
            String pwd = ageContent.getText().trim();
            //验证登录信息
            if (StringUtils.isEmpty(name) || StringUtils.isEmpty(pwd)) {
                PlugsUtils.modalMessageWarn("请完整填写您的账户密码");
            } else {
                JSONObject askParams = new JSONObject() {{
                    put("username", name);
                    put("password", pwd);
                }};
                String post = HttpUtils.post(askParams, null, REQ_HTTP_CHECK_LOGIN);
                if ("true".equals(post.trim())) {
                    //保存当前用户信息
                    WindowsRem.MY = name;
                    System.setProperty(SYS_USER_KEY, name);
                    //打开通讯窗口
                    new ToolWindowsFactory().reOpen();
                    //关闭登录浮窗
                    loginInDialog.close(DialogWrapper.OK_EXIT_CODE);
                }else{
                    PlugsUtils.modalMessageError("账户或密码错误，请重试，忘记密码请详询官网");
                }
            }
        });

        return south;
    }
}